home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / testglue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-05  |  3.2 KB  |  157 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #include <events.h>
  3.  
  4. #include "tcpglue.h"
  5.  
  6. static UDPiopb pb,rpb,wpb;
  7.  
  8. static xCleanup(status) int status;
  9. {
  10.     (void) xUDPRelease(&pb);
  11. }
  12.  
  13. /*
  14.  * UDP asynchronous notification routine
  15.  */
  16. static int notified = 0;
  17. static int lastNotifyCount = 0;
  18.  
  19. static StreamPtr notifyUdpStream;
  20. static unsigned short notifyEventCode;
  21. static Ptr notifyUserDataPtr;
  22. static unsigned short notifyTerminReason;
  23. static struct ICMPReport *notifyIcmpMsg;
  24.  
  25. pascal void sock_udp_notify(udpStream,eventCode,userDataPtr,icmpMsg)
  26.     StreamPtr udpStream;
  27.     unsigned short eventCode; 
  28.     Ptr userDataPtr;
  29.     struct ICMPReport *icmpMsg;
  30. {
  31.     notified++;
  32.  
  33.     notifyUdpStream = udpStream;
  34.     notifyEventCode = eventCode;
  35.     notifyUserDataPtr = userDataPtr;
  36.     notifyIcmpMsg = icmpMsg;
  37. }
  38.  
  39. static char *eventNames[] = 
  40. {
  41.     "event 0",
  42.     "data arrival",
  43.     "ICMP message"
  44. };
  45. static char *icmpMessages[] =
  46. {
  47.     "net unreachable",
  48.     "host unreachable",
  49.     "protocol unreachable",
  50.     "port unreachable",
  51.     "fragmentation required",
  52.     "source route failed",
  53.     "time exceeded",
  54.     "parameter problem",
  55.     "missing required option"
  56. };
  57. udpCheckNotify()
  58. {
  59.     if (notified == lastNotifyCount)
  60.         return(0);
  61.     
  62.     lastNotifyCount = notified;
  63.     dprintf("notify count is now %d\n",lastNotifyCount);
  64.     dprintf("stream %08x\n",notifyUdpStream);
  65.     dprintf("event %d '%s'\n",notifyEventCode,eventNames[notifyEventCode]);
  66.     if (notifyEventCode == UDPDataArrival)
  67.         dprintf("%d bytes\n",notifyTerminReason/*!?*/);
  68.     dprintf("icmp msg %08x\n",notifyIcmpMsg);
  69.     if (notifyEventCode == UDPICMPReceived)
  70.     {
  71.         dprintf("stream %08x\n",notifyIcmpMsg->streamPtr);
  72.         dprintf("local %08x/%d\n",notifyIcmpMsg->localHost,notifyIcmpMsg->localPort);
  73.         dprintf("remote %08x/%d\n",notifyIcmpMsg->remoteHost,notifyIcmpMsg->remotePort);
  74.         dprintf("%s\n",icmpMessages[notifyIcmpMsg->reportType]);
  75.         dprintf("optionalAddlInfo %04x\n",notifyIcmpMsg->optionalAddlInfo);
  76.         dprintf("optionalAddlInfoPtr %08x\n",notifyIcmpMsg->optionalAddlInfoPtr);
  77.     }
  78.     dprintf("userdata %s\n",notifyUserDataPtr);
  79.     return(1);
  80. }
  81.  
  82. /*
  83.  * UDP io completion routine
  84.  */
  85. static OSErr UDPResult;
  86. static long rhost;
  87. static short rport;
  88. static int len;
  89.  
  90. static void UDPDone(pb) 
  91.     struct UDPiopb *pb;
  92. {
  93.     UDPResult = pb->ioResult;
  94.     if (UDPResult != noErr)
  95.         return;
  96.         
  97.     switch (pb->csCode)
  98.     {
  99.         case UDPRead:
  100.             len = pb->csParam.receive.rcvBuffLen;
  101.             break;
  102.     }
  103. }
  104.  
  105. static OSErr io;
  106. static char rcvbuf[1024];
  107. static char sendbuf[1024];
  108. wdsEntry wds[2];
  109.  
  110. main()
  111. {
  112.     atexit(xCleanup);
  113.     
  114.     if ((io = xUDPCreate(8192, sock_udp_notify, 0, &pb)) != noErr)
  115.     {
  116.         fprintf(stderr,"UDPCreate failed code %d\n",io);
  117.         exit(1);
  118.     }
  119.     fprintf(stderr,"udpStream %08x\n",pb.udpStream);
  120.  
  121.     udpCheckNotify();
  122.  
  123. #if 0
  124.     rpb.udpStream = pb.udpStream;
  125.     if ((io = xUDPRead(&rpb, -1L)) != noErr) 
  126.     {
  127.         fprintf(stderr,"xUDPRead failed code %d\n",io);
  128.         exit(1);
  129.     }
  130.  
  131.     udpCheckNotify();
  132. #endif
  133.  
  134.     wpb.udpStream = pb.udpStream;
  135.     strcpy(sendbuf,"HELO milligan.utcs.utoronto.ca");
  136.     wds[0].length = strlen(sendbuf);
  137.     wds[0].ptr = sendbuf;
  138.     wds[1].length = 0;
  139.     if ((io = xUDPWrite(&wpb, 0x8064660a,69, &wds[0], 0L)) != noErr)
  140.     {
  141.         fprintf(stderr,"xUDPWrite failed code %d\n",io);
  142.         exit(1);
  143.     }
  144.  
  145.     for(;;)
  146.     {
  147.         udpCheckNotify();
  148.     }
  149.  
  150.     if ((io = xUDPRelease(&pb)) != noErr)
  151.     {
  152.         fprintf(stderr,"xUDPRelease failed code %d\n",io);
  153.         exit(1);
  154.     }
  155. }
  156.  
  157.